using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using ApiTest.Api.Entity;
using System.Linq;
using ApiTest.Api.Db;
using ApiTest.Api.Repository;
using ApiTest.Api.Params;
using Microsoft.Extensions.Configuration;
using ApiTest.Api.Utils;
using Microsoft.AspNetCore.Authorization;
using Admin3000.Backend.Api.Params;

namespace ApiTest.Api.Controllers
{
    // [Authorize]
    [ApiController]
    [Route("[controller]")]
    public class StoreController : ControllerBase
    {
        private IConfiguration _configuration;
        private IRepository<Spu> _spuRepository;
        private IRepository<Subclassify> _subclassifyRepository;
        private IRepository<Classify> _classifyRepository;
        private IRepository<Spuspecification> _spuspecificationRepository;
        private IRepository<Spuspecificationvalue> _spuspecificationvalueRepository;
        private IRepository<SpuImage> _spuImageRepository;
        private TokenParameter _tokenParameter;

        public  StoreController(IConfiguration configuration, 
        IRepository<Spu> spuRepository,
        IRepository<Subclassify> subclassifyRepository,
        IRepository<Classify> classifyRepository,
        IRepository<Spuspecification> spuspecificationRepository,
        IRepository<Spuspecificationvalue> spuspecificationvalueRepository,
        IRepository<SpuImage> spuImageRepository
        )
        {
            _configuration = configuration;
          _spuRepository = spuRepository;
          _subclassifyRepository = subclassifyRepository;
          _classifyRepository = classifyRepository;
          _spuspecificationRepository = spuspecificationRepository;
          _spuspecificationvalueRepository = spuspecificationvalueRepository;
          _spuImageRepository = spuImageRepository;
            _tokenParameter = _configuration.GetSection("TokenParameter").Get<TokenParameter>();
        }

        //商品详情
        [HttpGet("{spuid}")]
        public dynamic Get(int spuid)
        {
            var spu=_spuRepository.Table.Where(x=>x.Id==spuid).ToList();
            var subclassify=_subclassifyRepository.Table.ToList();
            var classify=_classifyRepository.Table.ToList();
            var spuspecification=_spuspecificationRepository.Table.Where(x=>x.SpuId==spuid).ToList();
            var spuspecificationvalue=_spuspecificationvalueRepository.Table.ToList();
            var spuImage=_spuImageRepository.Table.ToList();
            
             var Vaule=(from a in spu
                    join b in spuspecification on a.Id equals b.SpuId
                   
                    join d in spuspecificationvalue on b.Id equals d.SpuspecificationId
                   
                    select new
                    {
                          Brand = a.Brand,
                        SpuId = a.Id,
                        Intro = a.Intro,
                        Num = a.Num,
                        Price = a.Price,
                        Spuname = a.Spuname,
                        SubclassifyId = a.SubclassifyId,
                        spuspecificationnameId=b.Id,
                        spuspecificationId = d.SpuspecificationId,
                        spuspecificationname=b.Spuspecificationname,
                        spuspecificationvaluenameId=d.Id,
                        spuspecificationvaluename=d.Spuspecificationvaluename,
                       
                        
                        
                    });
                //  var sameAges = Vaule.GroupBy(g => new{g.spuspecificationname}).Select(g=>g.First()).ToList();

           
                 var Image=from a in spu
                    
                    join c in spuImage on a.Id equals c.SpuId
                    
                    select new
                    {
                        
                        ImageId=c.ImageId,
                        
                        
                    };
              

                return new{
                    Code=1000,
                    Data=new{Spu=spu,Vaule=Vaule,Spus=new{spuspecification=spuspecification,Image=Image}},
                    Msg="查询成功"
                };
            
        }
        //商品列表
        [HttpGet]
        public dynamic GetList()
        {
            
            var spu=_spuRepository.Table.ToList();
           

            return new {
                Code=1000,
                Data=new{Spu=spu},
                Msg="获取商品列表成功"
            };
        }
        //商品前五条列表
        [HttpGet,Route("five")]
        public dynamic GetList7()
        {
            
            var spu=_spuRepository.Table.Take(5).ToList();
           

            return new {
                Code=1000,
                Data=spu,
                Msg="获取商品列表成功"
            };
        }
                //商品前三条列表
        [HttpGet,Route("three")]
        public dynamic GetList10()
        {
            
            var spu=_spuRepository.Table.Take(3).ToList();
           

            return new {
                Code=1000,
                Data=spu,
                Msg="获取商品列表成功"
            };
        }
        //商品列表倒数3条
        [HttpGet,Route("daothree")]
        public dynamic GetList11()
        {
            var spuImage=_spuImageRepository.Table.ToList();
            var spu=_spuRepository.Table.OrderByDescending(s => s.Id).Take(3);
            //  var Image=from a in spu
                    
            //         join c in spuImage on a.Id equals c.SpuId
                    
            //         select new
            //         {
                        
            //             ImageId=c.ImageId,
                        
                        
            //         };

            return new {
                Code=1000,
                Data=spu,
                Msg="获取商品列表成功"
            };
        }
        //商品列表倒数5条
        [HttpGet,Route("suiji")]
        public dynamic GetList6()
        {
            var spuImage=_spuImageRepository.Table.ToList();
            var spu=_spuRepository.Table.OrderByDescending(s => s.Id).Take(5);
            //  var Image=from a in spu
                    
            //         join c in spuImage on a.Id equals c.SpuId
                    
            //         select new
            //         {
                        
            //             ImageId=c.ImageId,
                        
                        
            //         };

            return new {
                Code=1000,
                Data=spu,
                Msg="获取商品列表成功"
            };
        }
        //商品大分类列表
        [HttpGet,Route("classify")]
        public dynamic GetList1()
        {
            var classify=_classifyRepository.Table.ToList();

            return new{
                Code=1000,
                Data=classify,
                Msg="查询规格成功"
            };
        }
        // //根据商品大分类Id找出对应商品列表
        [HttpGet("classify/{id}"),Route("classify/{id}")]
        public dynamic GetList2(int id)
        {
            var classify=_classifyRepository.Table.ToList();
            var subclassify=_subclassifyRepository.Table.ToList();
            var spu=_spuRepository.Table.ToList();



            var query=from a in classify 
                    join b in subclassify on a.Id equals b.ClassifyId
                    join c in spu on b.Id equals c.SubclassifyId

                    select new{
                        Classifyid=a.Id,
                        Supid=c.Id,
                        Spuname=c.Spuname,
                        Spuprice=c.Price,
                        Spubrand=c.Brand,
                        Spuintro=c.Intro,
                        Shopid=c.ShopId,
                        ImageId=c.ImageId
                    };
            var Spu=query.Where(x=>x.Classifyid==id).ToList();

            return new 
            {
                Code=1000,
                Data=Spu,
                Msg="查询成功"
            };

        }     

           // //根据商品大分类Id找出对应细分类列表
        [HttpGet("subclass/{id}"),Route("subclass/{id}")]
        public dynamic GetList5(int id)
        {
            var classify=_classifyRepository.Table.ToList();
            var subclassify=_subclassifyRepository.Table.ToList();
            // var spu=_spuRepository.Table.ToList();



            var query=from a in classify 
                    join b in subclassify on a.Id equals b.ClassifyId
                    // join c in spu on b.Id equals c.SubclassifyId

                    select new{
                        SubclassifyId=b.Id,
                        Subclassifyfyname=b.Subclassifyfyname,
                        ClassifyId=b.ClassifyId,
                    };
            var Spu=query.Where(x=>x.ClassifyId==id).ToList();

            return new 
            {
                Code=1000,
                Data=Spu,
                Msg="查询成功"
            };

        }  



         // //根据商品细分类Id找出对应商品列表
        [HttpGet("subclassify/{id}"),Route("subclassify/{id}")]
        public dynamic GetList3(int id)
        {
            // var classify=_classifyRepository.Table.ToList();
            var subclassify=_subclassifyRepository.Table.ToList();
            var spu=_spuRepository.Table.ToList();



            var query=from a in spu
                    join b in subclassify on a.SubclassifyId equals b.Id
                    

                    select new{
                        Subclassifyid=b.Id,
                        Supid=a.Id,
                        Spuname=a.Spuname,
                        Spuprice=a.Price,
                        Spubrand=a.Brand,
                        Spuintro=a.Intro,
                        Shopid=a.ShopId,
                        Imageid=a.ImageId
                    };
            var Spu=query.Where(x=>x.Subclassifyid==id).ToList();

            return new 
            {
                Code=1000,
                Data=Spu,
                Msg="查询成功"
            };

        }  

         // //根据商品规格Id找出对应规格值列表
        [HttpGet("fw/{id}"),Route("fw/{id}")]
        public dynamic GetList4(int id)
        {
            var cation=_spuspecificationRepository.Table.ToList();
            var cationvalue=_spuspecificationvalueRepository.Table.ToList();
            



            var query=from a in cation
                    join b in cationvalue on a.Id equals b.SpuspecificationId
                    

                    select new{
                        SpuspecificationId=a.Id,
                        SpuspecificationvalueId=b.Id,
                        Spuspecificationvaluename=b.Spuspecificationvaluename

                    };
            var Spu=query.Where(x=>x.SpuspecificationId==id).ToList();

            return new 
            {
                Code=1000,
                Data=Spu,
                Msg="查询成功"
            };

        }  
        //商品名称模糊查询
        [HttpGet("select/{name}"),Route("select/{name}")]
        public dynamic GetList8(string name)
        {
           
            if(string.IsNullOrEmpty(name))
            {
                var NAME =_spuRepository.Table.ToList();
                return new{
                Code=500,
                Data=NAME,
                Msg="查询为空"
            };
            }
            else{
 var Name =_spuRepository.Table.Where(x=>x.Spuname.Contains(name)).ToList();
 return new{
                Code=1000,
                Data=Name,
                Msg="查询商品成功"
            };
            }
            
        }
   
        



        
        
        

        
       
    }
}